<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="供应商区域列表"
      row-key="id"
      :indent="20"
      :columns="columns"
      :request-api="getSupplierRegionList"
    >
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDialog('新增', initData)">
          新增供应商区域
        </el-button>
      </template>
      <template #isShow="scope">
        <el-switch v-model="scope.row.isShow" size="small" disabled />
      </template>
      <template #operation="scope">
        <el-button v-auth="'view'" type="primary" link :icon="EditPen" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDialog('编辑', scope.row)"> 编辑 </el-button>
        <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteCategory(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <SupplierRegionDialog ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="supplierRegion">
import { Inventory } from "@/api/interface";
import { createSupplierRegion, deleteSupplierRegion, editSupplierRegion, getSupplierRegionList } from "@/api/modules/inventory";
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import SupplierRegionDialog from "@/views/inventory/components/SupplierRegionDialog.vue";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";

const proTable = ref<ProTableInstance>();
// 新建默认数据
const initData = reactive({
  id: 0
});

const columns: ColumnProps<Inventory.ResSupplierRegionList>[] = [
  { type: "index", label: "序号", width: 80 },
  { sortable: true, prop: "areaCode", label: "供应商区域编码", minWidth: 140, align: "left", search: { el: "input" } },
  { sortable: true, prop: "name", label: "供应商区域名称", minWidth: 140, align: "left", search: { el: "input" } },
  { sortable: true, prop: "totalSupplier", label: "区域供应商数量", minWidth: 140, align: "left" },
  { sortable: true, prop: "createTime", label: "创建时间", minWidth: 164, align: "left" },
  { prop: "operation", label: "操作", width: 220, fixed: "right" }
];

const deleteCategory = async (params: Inventory.ResSupplierRegionList) => {
  await useHandleData(deleteSupplierRegion, { id: params.id }, `删除【${params.name}】供应商区域`);
  proTable.value?.getTableList();
};

const dialogRef = ref<InstanceType<typeof SupplierRegionDialog> | null>(null);
const openDialog = (title: string, row: Partial<Inventory.ResSupplierRegionList> = {}) => {
  const params = {
    title,
    row: { ...row },
    isView: title === "查看",
    api: title === "新增" ? createSupplierRegion : title === "编辑" ? editSupplierRegion : undefined,
    categories: proTable.value!.tableData,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
</script>
